package com.feng.fengDUtil;

import java.util.List;
import org.springframework.util.CollectionUtils;

public class Page<T> {

  public static final int DEFAULT_PAGE_SIZE = 20;
  private int pageSize = 20;
  private int pageIndex;
  private int pageTotal;
  private int totalCount;
  private List<T> data;

  public Page() {
  }

  public Page(int pageSize) {
    this.pageSize = pageSize;
  }

  public int getPageTotal() {
    return pageTotal;
  }

  public int getPageSize() {
    return pageSize;
  }

  public void setPageSize(int pageSize) {
    if (pageSize != this.pageSize) {
      this.pageSize = pageSize;
      caculate();
    }
  }

  public int getPageIndex() {
    return pageIndex;
  }

  public void setPageIndex(int pageIndex) {
    this.pageIndex = pageIndex;
  }

  public int getTotalCount() {
    return totalCount;
  }

  public void setTotalCount(int totalCount) {
    this.totalCount = totalCount;
    caculate();
  }

  public void setPageTotal(int pageTotal) {
    this.pageTotal = pageTotal;
  }

  public List<T> getData() {
    return data;
  }

  public void setData(List<T> data) {
    this.data = data;
  }

  private void caculate() {
    if (pageSize != 0) {
      pageTotal = (totalCount / pageSize + (totalCount % pageSize > 0 ? 1
          : 0));
    }
    if (pageTotal < 1) {
      pageTotal = 1;
    }
  }

  public boolean hasNextPageAndSetPageIndex() {
    if (CollectionUtils.isEmpty(data)) {
      return false;
    }
    if (pageTotal == 0) {
      pageIndex = 0;
      return true;
    }
    if (pageIndex + 1 >= pageTotal) {
      return false;
    }
    pageIndex += 1;
    return true;
  }
}